<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户</title>
    <link rel="stylesheet" type="text/css" href="../lib/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../lib/themes/icon.css">
	<link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
	<script type="text/javascript" src="../lib/jquery.min.js"></script>
	<script type="text/javascript" src="../lib/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../lib/datagrid-filter.js"></script>
	<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
	<script type="text/javascript" src="../lib/jiekou.js"></script>
	<script type="text/javascript" src="../lib/tools.js"></script>
	<script type="text/javascript" src="../lib/mask/jquery.loadmask.min.js"></script>
	<script type="text/javascript" src="../lib/locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript" src="../lib/base64.js"></script>
	<link href="../lib/mask/jquery.loadmask.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="../lib/mask/jquery.loadmask.min.js"></script>
</head>
<body>

	<div id="userwin" class="easyui-window" title="操作" closed="true" style="padding:30px 60px; width:40%;height:500px">

		<!-- start -->
		<form id="userff" method="post">
			<div  style="margin-bottom:20px">
				<input  type="hidden"  name="userid" />
				<input class="easyui-textbox" name="username"  style="width:100%" data-options="label:'用户名:'">
			</div>
			<div style="margin-bottom:20px">
				<input class="easyui-textbox" name="pwd"  style="width:100%" data-options="label:'密码:'">
			</div>
			<div  style="margin-bottom:20px">
				<input class="easyui-textbox" name="pwd2"  style="width:100%" data-options="label:'再次密码:'">
			</div>
			<div style="margin-bottom:20px">
				<select id="userselect" class="easyui-combobox" name="roleid" label="用户角色" style="width:100%">
					<!-- <option value="1">湖南</option> -->
				</select>
			</div>
		
			<label for="">用户状态</label><br>
			<hr>
		    <div style="margin-bottom:20px">
				<input class="easyui-radiobutton" name="status" value="1" label="启用:"><br><br>
				<input class="easyui-radiobutton" name="status" value="0" label="禁用:">	
	        </div>
			
		<div style="text-align:center;padding:5px 0;margin-top: 10px;">
			<a href="javascript:void(0)" class="easyui-linkbutton" onclick="user.submit()" style="width:80px" id="usersubmit">提交</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" onclick="user.clear()" style="width:80px">清理</a>
		</div>
	</form>
	
</div>
    <table id=detailedmappingGrid></table>
	<div id="usertab">
		<a href="javascript:void(0)" onclick="user.add()" class="easyui-linkbutton" data-options="iconCls:'icon-add'">新增</a>
		<input id="searchss" style="width: 320px;"></input>
		<div id="searchmm" style="width:320px">
			<div data-options="name:'username',iconCls:'icon-man'">用户名</div>
			<div data-options="name:'role',iconCls:'icon-ok'">角色</div>
		</div>
	</div>
    <script>
		$('#searchss').searchbox({
			searcher:function(value,name){
			$('#searchss').searchbox('setValue', '');
             if(value){
				 var obj={};
				 if(name=="username"){
                   obj={username:value};
				 }else{
					 obj={role:value};
				 }
				 $("#detailedmappingGrid").datagrid("load",obj);
				
			 }else{
				$("#detailedmappingGrid").datagrid("load",{});
			 }
			},
			menu:'#searchmm',
			prompt:'输入文本值'
		});
		var user={
            add:function(){
				$('#userff').form('clear');
				user.setRoleids();
			},
			submit:function(){
            //start
				$("#userwin").mask("数据加载中...");
				$('#userff').form('submit',{
					iframe: false,
					url:Network.geturl("/v1/adduser"),
					onSubmit: function(param){
						//补充参数
						return true;	
					}, 
					success:function(res){
						$("#userwin").unmask();
						var res = eval('(' + res + ')');
						console.log(res);
						if(res.code==200){
							$('#userwin').window('close');
							$("#detailedmappingGrid").datagrid("reload");
						} else{
							$.messager.alert('提示','失败啊'+res.msg);
						}

					},
					error:function(result){
						$("#stuwin").unmask();
						console.log("==err");
						console.log(result);
					}
				});
			//end
			},
			clear:function(){
				//start
			  $('#userff').form('clear');
			   //end
			},
			update:function(param){
				//start
				
				this.setRoleids(function(p){
					
					var mm = Base64.decode(param);
			    	var yy=JSON.parse(mm);
					console.log("===>jinrule",yy);
					$('#userff').form('load',yy);
				});
				//end
				
			},
			delete:function(param){
				//start
				var username=localStorage.getItem("loginname");
				var mm = Base64.decode(param);
			    var yy=JSON.parse(mm);
				console.log(username,yy.username);
				if(username ==yy.username){
					$.messager.alert('提示',username+"为当前用户,已经登录无法删除");
					return
				}
				if( yy.username == "admin"){
					$.messager.alert('提示',"admin为超级用户,不能删除");
					return
				}
				$.messager.confirm('确认', "确定要删除吗？", function(r){

						if(r){
							//begin
							$("#userwin").mask("数据加载中...");
							$.ajax({
								url:Network.geturl("/v1/deleteuser"),
								type:'post',
								data:{
									userid:yy.userid+""
								},
								error:function(err){
									$("#userwin").unmask();
									$.messager.alert('提示','请求失败!'+err);	
								},
								success:function(res){
									$("#userwin").unmask();
									if(res.code==200){
										$("#detailedmappingGrid").datagrid("reload");
									} else{
										$.messager.alert('提示','失败啊'+res.msg);
									}
									
								}
							});

							//end
						}
					});

				//end
			},
			setRoleids:function(callback){
				 //BEGIN	
				 $("#detailedmappingGrid").mask("数据加载中...");
				 axios.post(Network.geturl("/v1/listroles"), {})
					.then(function (response) {
						$("#detailedmappingGrid").unmask();
						
						if(response.data.rows){
						
							var datas=response.data.rows;	
							var resultArray=[];
							for(var i=0;i<datas.length;i++){
								var obj={"id":datas[i].id,"text":datas[i].name+"--"+datas[i].id};
								resultArray.push(obj);
							}
			
							console.log(resultArray);
							if(resultArray.length ==0){
								$.messager.alert("添加失败","请先添加角色");
								return;
							}
							
							$('#userwin').window('open');
							$("#userselect").combobox({
							data:resultArray,
							valueField:'id',
							textField:'text'});
							if(callback){
								$('#usersubmit').linkbutton({
								text: '修改'
							    });
								callback("p");
							}else{
								$('#usersubmit').linkbutton({
								text: '新增'
							    });
								
							}	
							}else{
							$.messager.alert("数据异常",response.data.msg);
						}
									
					})
					.catch(function (error) {
						$("#detailedmappingGrid").unmask();
						$('#win').window('close');
						$.messager.alert("交易报错",error);
				});
				 //END
			}

		}
	//start
	var dg = $("#detailedmappingGrid").datagrid({
	    url:Network.geturl("/v1/listuser"),
		dataType:"json",
		pagination: true,
		rownumbers: true,
		pagination:true,  
		fit:true, 
		singleSelect:true,
		nowrap: true, 
		striped:true,
		fitColumns:true,
		striped:true,
		pageSize:10,
		pageList:[10,15,20,25,30,35],
		columns:[[
			{field:'userid',title:"id序列",width:'10%',align:'center'},
			{field:'username',title:"用户名",width:'15%',align:'center'},
            {field:"rolename",title:"角色",width:'15%',align:'center'},
            {field:"status",title:"用户状态",width:'10%',align:'center',formatter:  
				function(value,row,index){  
					if (row.status=="1"){
						return "正常";
					}
					return "禁用";
				}
			},
			{field:"lastlogin",title:"上次登陆时间",width:'20%',align:'center',formatter:  
				function(value,row,index){  
					var timestamp = row.lastlogin+"";
					var d = new Date(timestamp * 1000);    //根据时间戳生成的时间对象
					var date = (d.getFullYear()) + "-" + 
							(d.getMonth() + 1) + "-" +
							(d.getDate()) + " " + 
							(d.getHours()) + ":" + 
							(d.getMinutes()) + ":" + 
							(d.getSeconds());
					return date;
				}
			},
			{field:'x',title:'操作', width:'30%',
			formatter: function(value,row,index){
				var reslut=JSON.stringify(row);
				reslut=Base64.encode(reslut);
				var html = "";
				html += "&nbsp;<i class=\"fa fa-pencil-square-o\" style=\"color:#0767c8;font-size:16px;\" aria-hidden=\"true\" title=\"编辑\"  onclick=\"user.update('"+reslut+"')\"></i>";
				html += "&nbsp;<i class=\"fa fa-trash\" aria-hidden=\"true\" style=\"color:red;font-size:16px;\" title=\"删除\" onclick=\"user.delete('"+reslut+"')\"></i>"; 
		        return html;
				
			}
		}
			]],
			// toolbar: [{
		    // 	text: '<a href="#" class="easyui-linkbutton" data-options="iconCls:\'icon-add\'"></a>',
		    //     handler: function () { 
			// 		$('#userff').form('clear');
			// 		user.setRoleids();
		    //     }
		        
		    // }],
			toolbar:"#usertab",
		
			onLoadError:function(msg){

             console.log(msg)
		    },
		    onLoadSuccess:function(data){
				console.log("==>data")
				console.log(data)  
		    } 
});
    </script>
</body>
</html>